Why `is_base_of` works with private inheritance?
Posted
by Alexey Malistov
on Stack Overflow
See other posts from Stack Overflow
or by Alexey Malistov
Published on 2010-05-26T07:43:28Z
Indexed on
2010/05/26
8:31 UTC
Read the original article
Hit count: 218
Why the following code works?
typedef char (&yes)[1];
typedef char (&no)[2];
template <typename B, typename D>
struct Host
{
operator B*() const;
operator D*();
};
template <typename B, typename D>
struct is_base_of
{
template <typename T>
static yes check(D*, T);
static no check(B*, int);
static const bool value = sizeof(check(Host<B,D>(), int())) == sizeof(yes);
};
//Test sample
class B {};
class D : private B {};
//Exspression is true.
int test[is_base_of<B,D>::value && !is_base_of<D,B>::value];
Note that B
is private base. Note that operator B*()
is const.
How does this work? Why this works?
Why static yes check(D*, T);
is better than static yes check(B*, int);
?
© Stack Overflow or respective owner